//
//  XiaoKaiUserConfigViewController.m
//  Project_A
//
//  Created by 凯 on 15/7/22.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "XiaoKaiUserConfigViewController.h"
#import "BaseHeader.h"
#import "VSLaLaViewController.h"
#import "VSTableViewCell.h"
#import "DataBase.h"

@interface XiaoKaiUserConfigViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic , retain)UIButton *back;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation XiaoKaiUserConfigViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:219 / 255.0 blue:219 / 255.0 alpha:1];
    [self creatNav];
    // 创建视图tableView
    [self createTableView];
}
-(void)creatNav
{
    UIView *naview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20 )];
    naview.backgroundColor = NAVCOLOR;
    [self.view addSubview:naview];
    UILabel *Navlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    Navlabel.backgroundColor = NAVCOLOR;
    Navlabel.text = @"设置";
    Navlabel.font = [UIFont systemFontOfSize:22];
    Navlabel.textColor = [UIColor whiteColor];
    Navlabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:Navlabel];
    [self.view bringSubviewToFront:Navlabel];
    [self getbutton];
    
   
    
}
-(void)getbutton
{
    self.back = [UIButton buttonWithType:UIButtonTypeCustom];
    self.back.backgroundColor = [UIColor clearColor];
    self.back.frame = CGRectMake(5, 25, 32, 32);
    [self.back setBackgroundImage:[UIImage imageNamed:@"iconfont-xiangzuo@2x"] forState:UIControlStateNormal];
    [self.back addTarget:self action:@selector(getback) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.back];
    [self.view bringSubviewToFront:self.back];
    
    
    
}
-(void)getback
{
    [self.navigationController popViewControllerAnimated:YES];
    //    NSLog(@"走走走 回家喽");
    
}

/// VS添加
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 124) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:219 / 255.0 blue:219 / 255.0 alpha:1];
    self.tableView.tag = 20000;
    self.tableView.delegate  = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 3;
    } else if (section == 2) {
        return 3;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];
    if (!cell) {
        cell = [[[VSTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"TableCell"] autorelease];
    }
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    cell.backgroundColor = [UIColor whiteColor];
    cell.section = indexPath.section;
    cell.row = indexPath.row;
    cell.myswitch.tag = 30000;
    [cell.myswitch addTarget:self action:@selector(setOn:animated:) forControlEvents:UIControlEventValueChanged];
    cell.memory = [NSString stringWithFormat:@"%.2fM", [XiaoKaiUserConfigViewController folderSizeAtPath:path]];
    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    return cell;
    
}

- (void)setOn:(BOOL)on animated:(BOOL)animated
{
    NSLog(@"lalal");
    UISwitch *t = (UISwitch *)[self.view viewWithTag:30000];
    BOOL isButtonOn = [t isOn];
    CGFloat a = 0;
    if (isButtonOn) {
        a = 1;
        NSLog(@"aaaaaaaaaaaaaa");
    } else {
        a = 0.618;
         NSLog(@"aaaaaaaaaaaaaa");
    }
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"alpha" object:[NSString stringWithFormat:@"%f",a] userInfo:@{@"name": @"NotificationCenter"}];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UILabel *view1 = [[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 - SCREEN_HEIGHT / 5, 200, 80)] autorelease];
        view1.backgroundColor = [UIColor blackColor];
        view1.text = @"当前已是最高版本!";
        view1.font = [UIFont systemFontOfSize:20];
        view1.textColor = [UIColor whiteColor];
        [view1.layer setCornerRadius:10];
        view1.clipsToBounds = YES;
        view1.alpha = 0.5;
        view1.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:view1];
        [self.view bringSubviewToFront:view1];
        [UIView animateWithDuration:2.0f animations:^{
            view1.alpha = 0;
        }];
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        dispatch_async(
                       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                       , ^{
                           NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                           NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                           NSLog(@"files :%lu",[files count]);
                           for (NSString *p in files) {
                               NSError *error;
                               NSString *path = [cachPath stringByAppendingPathComponent:p];
                               if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                                   [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                               }
                           }
                           dispatch_queue_t mainQueue = dispatch_get_main_queue();
                           // 在主线程队列中进行
                           dispatch_sync(mainQueue, ^{
                               // 显示图片
                               UITableView *t = (UITableView *)[self.view viewWithTag:20000];
                               [t reloadData];
                           });
                       });
    }
    if (indexPath.section == 1 && indexPath.row == 2) {
        DataBase *dbManger = [DataBase shareInstance];
        [dbManger openDB];
        NSMutableArray *arr = [dbManger selectInfo];
        if (arr.count == 0) {
            UILabel *view1 = [[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 - SCREEN_HEIGHT / 5, 200, 80)] autorelease];
            view1.backgroundColor = [UIColor blackColor];
            view1.text = @"您没有收藏任何攻略!";
            view1.font = [UIFont systemFontOfSize:20];
            view1.textColor = [UIColor whiteColor];
            [view1.layer setCornerRadius:10];
            view1.clipsToBounds = YES;
            view1.alpha = 0.5;
            view1.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:view1];
            [self.view bringSubviewToFront:view1];
            [UIView animateWithDuration:2.0f animations:^{
                view1.alpha = 0;
            }];
        } else {
        [dbManger dropTable];
        [dbManger dropItemsTable];
        [dbManger closeDB];
        UILabel *view1 = [[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 - SCREEN_HEIGHT / 5, 200, 80)] autorelease];
        view1.backgroundColor = [UIColor blackColor];
        view1.text = @"清空收藏成功";
        view1.font = [UIFont systemFontOfSize:20];
        view1.textColor = [UIColor whiteColor];
        [view1.layer setCornerRadius:10];
        view1.clipsToBounds = YES;
        view1.alpha = 0.5;
        view1.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:view1];
        [self.view bringSubviewToFront:view1];
        [UIView animateWithDuration:2.0f animations:^{
            view1.alpha = 0;
            
        }];
        }
    }
    
    if (indexPath.section == 2 && indexPath.row != 2) {
        VSLaLaViewController *lala = [[VSLaLaViewController alloc]init];
        self.navigationController.navigationBar.hidden = YES;
        if (indexPath.row == 1) {
            lala.section_row = 1;
        }
        [self.navigationController pushViewController:lala animated:YES];
    }
    
    [self.tableView reloadData];
}

+ (float)fileSizeAtPath:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size / 1024.0 / 1024.0;
    }
    return 0;
}
+ (float)folderSizeAtPath:(NSString *)path
{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [XiaoKaiUserConfigViewController fileSizeAtPath:absolutePath];
        }
        
        return folderSize;
    }
    return 0;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
